angular.module('starter.controllers', [])

.controller('AppCtrl', function($scope, $ionicModal, $timeout) {
  // Form data for the login modal
  $scope.loginData = {};

  // Create the login modal that we will use later
  $ionicModal.fromTemplateUrl('templates/login.html', {
    scope: $scope
  }).then(function(modal) {
    $scope.modal = modal;
  });

  // Triggered in the login modal to close it
  $scope.closeLogin = function() {
    $scope.modal.hide();
  };

  // Open the login modal
  $scope.login = function() {
    $scope.modal.show();
  };

  // Perform the login action when the user submits the login form
  $scope.doLogin = function() {
    console.log('Doing login', $scope.loginData);

    // Simulate a login delay. Remove this and replace with your login
    // code if using a login system
    $timeout(function() {
      $scope.closeLogin();
    }, 1000);
  };
})


/*
*	Controller
*   -Popula lista de statements
*/
.controller('StatementListCtrl', function($scope, $ionicLoading, $http, StatementService ) {
	

	//Arranca spinner
//	$ionicLoading.show({
//		content: 'Loading',
//		animation: 'fade-in',
//		showBackdrop: true,
//		maxWidth: 200,
//		showDelay: 0
//	});
	
	
//	StatementService.getAll()
//		.success(function (data){
//			$scope.statements=data;
//			$ionicLoading.hide();
//		})
//		.error(function(data, status, headers, config) {
//			console.log('data' + status);
//			$ionicLoading.hide();
//		});  
	
	alert("ANTES DE LLAMAR AL SERVER");
	
	$http.get("http://192.168.0.7:8080/tesis/statements/").success(function(data, status, headers, config){
		alert("SUCCES:" + data)
		$scope.statements=data;
//		$ionicLoading.hide();

		
	}).error(function(data, status, headers, config) {
				alert('data' + status);
//				$ionicLoading.hide();
		}
	);	
	
	
		
}) 



/*
*	Controller
*	
*	Popular la lista de statements dado un grupo
*/
.controller('StatementXGroupCtrl', function($scope, $http, $stateParams, ActorService, StatementService) {
	
	 StatementService.getXGroup($stateParams.groupId)
		.success(function (data, status, headers, config) {
			$scope.statements = data;
		})
		.error(function(data, status, headers, config) {
			console.log('Error al traer statements ' + status);
		}
	);
	

}) 





/*
*  Controller  para crear un statenent
*
*/
.controller('StatementCreateCtrl', function($scope, $stateParams, $http, StatementService) {
		
	//model 
	$scope.statement = { "nombre": "",
						"descripcion": "",
						"adjunto":"",
						"team":"",
						"attachment":""};
	
	//Accion del submit. 
	$scope.put=function(){
		StatementService.put(StatementService.createDao($scope));
	};
})



/*
*  Controller  para crear un statenent
*
*/
.controller('StatementCreateExtensionCtrl', function($scope, $stateParams, $http, StatementService) {
	
	console.log("entro  a StatementCreateExtensionCtrl");
	
	$scope.statementParentId=$stateParams.statementParentId;
	
	//model 
	$scope.statement = { "nombre": "",
						"descripcion": "",
						"adjunto":"",
						"team":"",
						"attachment":""};
	
	//Accion del submit. 
	$scope.put=function(){
		StatementService.putExtension($stateParams.statementParentId,StatementService.createDao($scope));
	};
})




/*
*  Controller  
*
* Crea un statement y lo agrega al contexto del statement padre.
*
*
*/
.controller('StatementExtensionCtrl', function($scope, $stateParams, $http, StatementService) {
	
	var statementParentId = $stateParams.statementParentId;
	$scope.statementParentId=id;
	
	//model 
	$scope.statement = { "nombre": "",
						"descripcion": "",
						"adjunto":"",
						"team":"",
						"adjunto":""};
	
	//Accion del submit. 
	$scope.put=function(){
		StatementService.putExtension(StatementService.createDao($scope));
	};
})


/*
*Controller: carga lista de grupos
*/
.controller('GroupCtrl', function($scope, $http, ActorService) {
	 
	//
	  ActorService.getAllGroup()
	  .success(function (data, status, headers, config) {
			$scope.groups=data;
	  })
	  .error(function(data, status, headers, config) {
			console.log('Error en get de grupos ' + status);
	  });	
})


/*
*	Controller
*	
*	Obtiene un statemet por id y lo popula en la vista.
*		
*/
.controller('StatementCtrl', function($scope, $stateParams, $http, $window,$state, StatementService) {
	
	//
	 StatementService.get($stateParams.statementId)
		.success(function (data, status, headers, config) {
				console.log(data)
				$scope.statement = data;
		})
		.error(function(data, status, headers, config) {
			console.log('data' + status);	
		});
	
	
	//Redirecciona al template statementcreate.html.
	$scope.go = function() {
		console.log("Entro al action, con parametro " + $stateParams.statementId);
		$state.go('app.substatementcreate', { "statementParentId": $stateParams.statementId} );
	  };

	  
	$scope.viewAttachment=function(){
		
		console.log("Entro al action, con parametro " + $scope.statement.id);
		$state.go('app.attachmentviewer', { "statementId": $scope.statement.id} );
		
	}  
	  
	
})



/*
*	Controller
*	
*	Visualiza el attachment.
*		
*/
.controller('AttachmentViewerCtrl', function($scope, $stateParams, $http, $sce, $window,$state, StatementService, UtilService) {
	
	$scope.showVideo=false;
	$scope.showLink=false;
	var statement;
	console.log("attach " + $stateParams.statementId);
	
	//$sceDelegateProvider.resourceUrlWhitelist(['self', 'https://player.vimeo.com/video/**']);
	
	
    StatementService.get($stateParams.statementId)
		.success(function (data, status, headers, config) {
				console.log(data)
				$scope.statement = data;
		
			console.log(data.attachment.contentType);
			console.log(data.attachment.fileURL);

			if(data.attachment.contentType == "video") {
				$scope.showVideo=true;
	
				$scope.video = $sce.trustAsResourceUrl( "http://www.youtube.com/embed/" + UtilService.parseYoutube(data.attachment.fileURL) ); 
			}
			 if(data.attachment.contentType == "url") {
					$scope.showLink=true;
			 }
		
		})
		.error(function(data, status, headers, config) {
			console.log('data' + status);	
		});
	
	  
		
	  
	  
	
})




;
